﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using HKEA.CMS.Core.BLL;
using HKEA.CMS.Core.Models;
using HKEA.CMS.Core.Utility;
using iMaker.Library;

namespace HKEA.CMS.Core.Utility
{
    public class MailManage
    {
       
        /// <summary>
        /// 析构函数
        /// </summary>
        public MailManage()
        {  
        }

        #region 内部辅助方法
        
        /// <summary>
        /// 获取Email的标题
        /// </summary>
        /// <param name="res_key">标题的资源key</param>
        /// <returns></returns>
        private static string GetEmailSubject(string res_key)
        {
            return ResourceManage.GetResourceString(res_key);
        }

        /// <summary>
        /// 获取邮件内容 传入必须的参数 渲染.
        /// </summary>
        /// <param name="content_html_name">邮件内容的模板名</param>
        /// <param name="args">HashTable的参数,key和邮件内容模板参数名称相对应</param>
        /// <returns>返回渲染后的模板内容</returns>
        private static string GetEmailContent(string content_html_name, Hashtable args)
        {
            string content = string.Empty;
            string templatePath = Constant.PathInfo.EmailFolder + Common.LanguageInfo.GetLanguage() + "/" + content_html_name;
            try
            {
                using (FileStream filestream = new FileStream(System.Web.HttpContext.Current.Server.MapPath(templatePath), FileMode.Open, FileAccess.Read))
                {
                    if (filestream.CanRead)
                        using (StreamReader reader = new StreamReader(filestream))
                            content = reader.ReadToEnd();
                }

                int args_count = content.Split(new string[1] { "{{" }, StringSplitOptions.RemoveEmptyEntries).Count()-1;
                if (args_count != args.Count)
                    throw new Exception("参数个数不匹配！");

                foreach (DictionaryEntry de in args)
                    content = content.Replace("{{"+de.Key.ToString()+"}}",de.Value.ToString());

                return content;
            }
            catch (Exception ex)
            {
                throw ex;
            }

        }

        #endregion

        #region 对外发送Email方法


        /// <summary>
        /// 忘记密码
        /// </summary>
        /// <param name="subject_resource_key">邮件标题 的 资源key.</param>
        /// <param name="content_html_name">邮件内容模板名.</param>
        /// <param name="to_email">给谁发送Email.</param>
        /// <param name="args">邮件内容HashTable参数 key名必须和模板的参数相对应.</param>
        public static void ForgetPassword(string subject_resource_key, string content_html_name, string to_email, Hashtable args)
        {
            //邮件标题
            string mail_subject = GetEmailSubject(subject_resource_key);
            //邮件内容
            string mail_content = GetEmailContent(content_html_name, args);

            ConfigurationManager cfg = new ConfigurationManager();

            MailSender.SendEmail(cfg.Host, to_email, mail_content, mail_subject, cfg.MailFrom, cfg.MailUserID, cfg.MailPW);
        }

        /// <summary>
        /// 尝试通用发送Email函数
        /// </summary>
        /// <param name="subject_resource_key">邮件标题 的 资源key.</param>
        /// <param name="content_html_name">邮件内容模板名.</param>
        /// <param name="to_email">给谁发送Email.</param>
        /// <param name="args">邮件内容HashTable参数 key名必须和模板的参数相对应.</param>
        public static bool CommonSendEmail(string subject_resource_key, string content_html_name, string to_email, Hashtable args)
        {
            //邮件标题
            string mail_subject = GetEmailSubject(subject_resource_key);
            //邮件内容
            string mail_content = GetEmailContent(content_html_name, args);

            ConfigurationManager cfg = new ConfigurationManager();

           string ret = MailSender.SendEmail(cfg.Host, to_email, mail_content, mail_subject, cfg.MailFrom, cfg.MailUserID, cfg.MailPW);

           if (string.IsNullOrEmpty(ret))
               return true;
           else
               return false;
        }


        #endregion

    }
}
